package cn.csust.net2.manager.client.panel;


import cn.csust.net2.manager.client.resources.Resources;
import cn.csust.net2.manager.client.ux.ExpandGrid;
import cn.csust.net2.manager.client.ux.ServiceCallback;
import cn.csust.net2.manager.shared.po.FileLoadNotification;
import cn.csust.net2.manager.shared.service.FileNoticServiceAsync;

import com.extjs.gxt.ui.client.Style.ButtonScale;
import com.extjs.gxt.ui.client.Style.IconAlign;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class UploadWindow extends Window{
	FileNoticServiceAsync service;
	private LayoutContainer layout;
	private ExpandGrid<FileLoadNotification> grid;
	private ContentPanel cp;
	public UploadWindow(FileNoticServiceAsync service){
		this.service = service;
		layout = new LayoutContainer();
		layout.setLayoutOnChange(true);
		layout.setLayout(new BorderLayout());
		
		init();
		this.show();
	}
	public void init(){
		this.setTitle("发送文件通知");
		this.setSize(500, 500);	 
		this.setLayout(new FitLayout());
		grid = new ExpandGrid<FileLoadNotification>(new FileLoadNotification(), service);
		grid.setColumnLines(true);
		cp = new ContentPanel();
		initTool();
		cp.setWidth("100%");
		cp.setHeight("80%");
		cp.setHeaderVisible(false);
		cp.setLayout(new FitLayout());
		cp.add(grid);
		cp.setBottomComponent(grid.getToolBar());
		layout.add(cp, new BorderLayoutData(LayoutRegion.CENTER));
		this.add(layout);
	}
	public void initTool(){
		/**
		 * 创建工具栏
		 */

		ToolBar tb = new ToolBar();
		// 增加和删除按钮，修改
		Button delete= new Button("删除");
		delete.setIconAlign(IconAlign.TOP);
		delete.setScale(ButtonScale.LARGE);
		delete.setIcon(AbstractImagePrototype.create(Resources.ICONS.details()));
		delete.addSelectionListener(new SelectionListener<ButtonEvent>() {

		  @Override
			public void componentSelected(ButtonEvent ce) {
						
			         delete();
				}
		});
		tb.add(delete);
		cp.setTopComponent(tb);
	}
	public void delete(){
		if(grid.getSelectionModel().getSelectedItems().size()!=1){
			MessageBox.alert("系统提示", "请选择一项药查看的内容", null);
		}
		MessageBox.confirm("警告", "你确定要删除所选项吗?",
				new Listener<MessageBoxEvent>() {

					@Override
					public void handleEvent(MessageBoxEvent be) {
						FileLoadNotification f =	grid.getSelectionModel().getSelectedItem().getBean();
						service.delete(f, new ServiceCallback<Void>() {
							
							@Override
							public void onSuccess(Void result) {
								
								MessageBox.alert("系统提示", "删除成功", null);
							}
							
						});
					}
				});
		
	}
}
